<?php
class Promosi extends CI_Controller{

    private $limit = 10;
	
	function  __construct() {
        parent::__construct();
        $this->load->model('Promosi_model');
        $this->load->model('User_model');
		$this->load->helper(array('form', 'url'));
		$this->load->library(array('form_validation','pagination'));
     }

	function index(){

        $page=$this->uri->segment(3);
	    $batas=10;
	    if(!$page):
	    $offset = 0;
	    else:
	    $offset = $page;
	    endif;

	    $data['nama']="";
	    $postkata = $this->input->post('nama');
	    if(!empty($postkata))
	    {
	        $data['nama'] = $this->input->post('nama');
	        $this->session->set_userdata('pencarian_promo10', $data['nama']);
	    }
	    else
	    {
	        $data['nama'] = $this->session->userdata('pencarian_promo10');
	    }
	    $data['nama_promo10'] = $this->Promosi_model->Cari_promo10($batas,$offset,$data['nama']);
	    $tot_hal = $this->Promosi_model->tot_hal('barang','strnama',$data['nama']);

	    $config['base_url'] = base_url() . 'promosi/index/';
	        $config['total_rows'] = $tot_hal->num_rows();
	        $config['per_page'] = $batas;
	        $config['uri_segment'] = 3;
	        $config['full_tag_open'] = '<div id="pagination">';
                $config['full_tag_close'] = '</div>';
                $this->pagination->initialize($config);
	        $this->pagination->initialize($config);
	    $data["pagination"] =$this->pagination->create_links();

	    $this->load->view('admin_views/promosi/promosi',$data);
    }
	
	function add()
	{
		$this->form_validation->set_rules('intid_week_start', 'Minggu Start', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_week_end', 'Minggu Akhir', 'trim|required|xss_clean');


		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		{
			
                $week = $this->Promosi_model->selectWeek();
				foreach ($week as $g)
				{
					$data['idw'][]	        = $g->intid_week;
					$data['ide'][]	        = $g->intid_week;
					
				}
			$this->load->view('admin_views/promosi/add', $data);
                        

		}else {
			$this->Promosi_model->insert($_POST);
			redirect('promosi/promosi');
		}
	}

    function lookupBarang(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Promosi_model->selectBarang($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}
	
    function lookupFree(){
	$keyword = $this->input->post('term');
        $data['response'] = 'false';
        $query = $this->Promosi_model->selectBarangFree($keyword);
        if( ! empty($query) )
        {
            $data['response'] = 'true';
            $data['message'] = array();
            foreach( $query as $row )
            {
                $data['message'][] = array(
                                        'id'=>$row->intid_barang,
                                        'value' => $row->strnama,
                                        ''
                                     );
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data);

        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data);
        }
	}

	function edit($intid_promo)
		{
			if($_POST==NULL)
			{
              $week = $this->Promosi_model->selectWeek();
				foreach ($week as $g)
				{
					$data['idw'][]	        = $g->intid_week;
					$data['ide'][]	        = $g->intid_week;
				}
                $datapromo= $this->Promosi_model->select($intid_promo);
				foreach ($datapromo as $g)
				{
					$data['intid_promo']	 	= $g->intid_promo;
					$data['intid_week_start'] 	= $g->intid_week_start;
					$data['intid_week_end'] 	= $g->intid_week_end;
					$data['intid_barang'] 		= $g->intid_barang;
					$data['intid_barang_free']  = $g->intid_barang_free;
					$data['strnama'] 			= $g->strnama;
					$data['nama']  				= $g->nama;
				}
				$this->load->view('admin_views/promosi/edit', $data);
			}else {
				$this->Promosi_model->update($intid_promo);
				redirect('promosi/index');
			}

		}
	

    function delete($intid_promo)
	{
		$this->Promosi_model->delete($intid_promo);
		redirect('promosi');
	}
  
function promosi2()
	{

        $page=$this->uri->segment(3);
	    $batas=10;
	    if(!$page):
	    $offset = 0;
	    else:
	    $offset = $page;
	    endif;

	    $data['nama']="";
	    $postkata = $this->input->post('nama');
	    if(!empty($postkata))
	    {
	        $data['nama'] = $this->input->post('nama');
	        $this->session->set_userdata('pencarian_promo20', $data['nama']);
	    }
	    else
	    {
	        $data['nama'] = $this->session->userdata('pencarian_promo10');
	    }
	    $data['nama_promo20'] = $this->Promosi_model->Cari_promo20($batas,$offset,$data['nama']);
	    $tot_hal = $this->Promosi_model->tot_hal2('barang','strnama',$data['nama']);

	    $config['base_url'] = base_url() . 'promosi/promosi2/';
	    $config['total_rows'] = $tot_hal->num_rows();
	    $config['per_page'] = $batas;
	    $config['uri_segment'] = 3;
        $config['full_tag_open'] = '<div id="pagination">';
        $config['full_tag_close'] = '</div>';
	    $this->pagination->initialize($config);
	    $data["pagination"] =$this->pagination->create_links();

	    $this->load->view('admin_views/promosi/promosi2',$data);
	}
        function add2()
	{
		$this->form_validation->set_rules('intid_week_start', 'Minggu Start', 'trim|required|xss_clean');
		$this->form_validation->set_rules('intid_week_end', 'Minggu Akhir', 'trim|required|xss_clean');


		$this->form_validation->set_message('required', ' %s tidak boleh kosong !!');

		if ($this->form_validation->run() == FALSE)
		{

                $week = $this->Promosi_model->selectWeek();
				foreach ($week as $g)
				{
					$data['idw'][]	        = $g->intid_week;
					$data['ide'][]	        = $g->intid_week;
					
				}
			$this->load->view('admin_views/promosi/add2', $data);

		}else {
			$this->Promosi_model->insert2($_POST);
			redirect('promosi/promosi2');
		}
	}

        function edit2($intid_promo)
		{
			if($_POST==NULL)
			{
                         $week = $this->Promosi_model->selectWeek();
				foreach ($week as $g)
				{
					$data['idw'][]	        = $g->intid_week;
					$data['ide'][]	        = $g->intid_week;
				}
                $datapromo2= $this->Promosi_model->select2($intid_promo);
				foreach ($datapromo2 as $g)
				{
					$data['intid_promo']	 	= $g->intid_promo;
					$data['intid_week_start'] 	= $g->intid_week_start;
					$data['intid_week_end'] 	= $g->intid_week_end;
					$data['intid_barang'] 		= $g->intid_barang;
					$data['intid_barang_free']  = $g->intid_barang_free;
					$data['intid_barang_free1'] = $g->intid_barang_free1;
					$data['intid_barang_free2'] = $g->intid_barang_free2;
					$data['strnama']      		= $g->strnama;
					$data['nama'] 				= $g->nama;
					
				}
				$this->load->view('admin_views/promosi/edit2', $data);
			}else {
				$this->Promosi_model->update2($intid_promo);
				redirect('promosi/promosi2');
			}

		}

    function delete2($intid_promo)
	{
		$this->Promosi_model->delete2($intid_promo);
		redirect('promosi/promosi2');
	}
	//promosi starterkit ABO
	/*
	* Tujuan	: untuk mentrack DEALER YANG MEMBELI penjualan starterkit abo.
	* Deskripsi	: Dengan mengambil periode starterkit ABO MAKA MENJADI PATOKAN PENJUALAN.
	*/
	function PROMOSI_STARTTERKIT_ABO(){
		$intid_cabang = $this->input->post('intid_cabang');
		$intid_week	=	$this->input->post('intid_week');
		
		$dataTemp['intid_week']	=	$intid_week;
		$dataTemp['intid_cabang']	=	$intid_cabang;
		//untuk starterkitnya dibuat manual
		$dataTemp['intid_starterkit']	=	6133;
		
		$var = "";
		$var_temp = "";
		
		$query	=	$this->Promosi_model->get_Promosi_starterkit_ABO($dataTemp);
		if($query->num_rows() > 0){
		///
		$a = $intid_week + 1;
		$b = $intid_week + 2;
		foreach($query->result() as $row){
			$cabang = $this->User_model->getCabang($this->session->userdata('username'));
			//$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
		
			$dataTemp1['intid_dealer']	=	$row->intid_dealer;
			$dataTemp1['intid_week']	=	$intid_week;
			$query_1	=	$this->Promosi_model->get_omset_week($dataTemp1);
			$total_omset1 = $query_1->result();
			
			$dataTemp2['intid_dealer']	=	$row->intid_dealer;
			$dataTemp2['intid_week']	=	$a;
			$query_2	=	$this->Promosi_model->get_omset_week($dataTemp2);
			$total_omset2 = $query_2->result();
			
			$dataTemp3['intid_dealer']	=	$row->intid_dealer;
			$dataTemp3['intid_week']	=	$b;
			$query_3	=	$this->Promosi_model->get_omset_week($dataTemp3);
			$total_omset3 = $query_3->result();
			
			$var_temp .= "<tr>
					<td style='padding:5px;'>".strtoupper($row->strnama_dealer)."</td>
					<td style='padding:5px;'>".strtoupper($row->strnama_upline)."</td>
					<td style='padding:5px;'>".$total_omset1[0]->inttotal_omset."</td>
					<td style='padding:5px;'>".$total_omset2[0]->inttotal_omset."</td>
					<td style='padding:5px;'>".$total_omset3[0]->inttotal_omset."</td>";
			$total = $total_omset1[0]->inttotal_omset + $total_omset2[0]->inttotal_omset + $total_omset3[0]->inttotal_omset;
			$var_temp .= "<td style='padding:5px;'>".$total."</td>";
			if($cabang[0]->intid_cabang == 1){
					if($total <= 800000){
				$var_temp .= "<td style='padding:5px;'><form method='POST' action='".base_url()."PROMOSI/promo_stepdown_dealer'>
				<input type='hidden' name='intid_dealer' value='".$row->intid_dealer."' />
				<input type='submit' name='buttonSubmit' value='step Down' /></form></td>";
					}else{
				$var_temp .= "<td style='padding:5px;'><csnter><i>SUCCESS</i></center></td>";
				}
			}
			$var_temp .="</tr>";
		}
		$var .= "<p><a href='".base_url()."Laporan'>BACK</a></p>";
		$var .= "<h3>WEEK PENCARIAN STARTERKIT ABO: ".$intid_week."</h3>";
		$var .= "<table border= 1 style='padding:5px;width:100%;'>
					<tr style='background:rgb(143,200,0);'>
						<th>NAMA KONSUMEN</th>
						<th>NAMA UPLINE MEMBER</th>
						<th>Omset week ".$intid_week."</th>
						<th>Omset week ".$a."</th>
						<th>Omset week ".$b."</th>
						<th>TOTAL</th>";
						if($cabang[0]->intid_cabang == 1){
							$var .= "<th>ACTION</th>";
						}
		$var .=	"</tr>";
		$var .= $var_temp;
		$var .= "</table>";
		}else{
			$var .= "<p>Tidak ada Penjualan Starterkit ABO di week-".$intid_week.". Untuk kembali silahkan klik <a href='".base_url()."Laporan'>disini</a></p>";
		}
		$data['view'] = $var;
		$this->load->view('halaman/PROMOSI',$data);
	}
	function promo_stepdown_dealer(){
		$intid_dealer = $this->input->post('intid_dealer');
		
		$var = "";
		$dataTemp['intid_dealer']	=	$intid_dealer;
		$query_3	=	$this->Promosi_model->get_nota_dealer($dataTemp);
		
		$dataTemp['intid_dealer']	=	$intid_dealer;
		$query_4	=	$this->Promosi_model->get_dealer($dataTemp);
		$member	=	$query_4->result();
		
			foreach($query_3->result() as $row){
				$var .= "NO NOTA : ".$row->intno_nota.' . terupdate untuk '.$member[0]->strnama_upline." ".$member[0]->intid_upline." : ".$intid_dealer."<br />";
				$dataTemp2['intid_dealer'] = $member[0]->intid_upline;
				$dataTemp2['intid_nota'] = $row->intid_nota;
				$this->Promosi_model->insert_nota_history_from_nota($dataTemp2);
				$this->Promosi_model->update_nota($dataTemp2);
			}
			$this->Promosi_model->delete_member($dataTemp);
			$var .= "<br /><a href='".base_url()."Laporan/'>BACK</a>";
		$data['view']	= $var;
		$this->load->view('halaman/PROMOSI',$data);
	}
}
?>
